package com.halfsword.creativecraft.block.custom.machine.blockentity.modules;

import com.halfsword.creativecraft.modules.interfaces.IItemHandlerFacet;
import net.minecraft.core.BlockPos;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.items.ItemStackHandler;

/**
 * 物品处理模块实现
 * 负责管理机器的物品槽位和物品处理逻辑
 */
public class ItemHandlerModule implements IItemHandlerFacet {

    private final String moduleId;
    private final ItemStackHandler itemHandler;
    private final int[] inputSlots;
    private final int[] outputSlots;

    public ItemHandlerModule(String moduleId, int slotCount, int[] inputSlots, int[] outputSlots) {
        this.moduleId = moduleId;
        this.itemHandler = createItemHandler(slotCount);
        this.inputSlots = inputSlots;
        this.outputSlots = outputSlots;
    }

    @Override
    public String getModuleId() {
        return moduleId;
    }

    @Override
    public void tick(Level level, BlockPos pos, BlockState state) {
        // 物品处理模块的tick逻辑
        // 可以在这里实现自动输入输出等逻辑
    }

    @Override
    public int[] getInputSlots() {
        return inputSlots;
    }

    @Override
    public int[] getOutputSlots() {
        return outputSlots;
    }

    @Override
    public ItemStackHandler getItemHandler() {
        return itemHandler;
    }

    protected ItemStackHandler createItemHandler(int size) {
        return new ItemStackHandler(size) {
            @Override
            protected void onContentsChanged(int slot) {
                // 当物品槽位内容变化时，可以在这里处理
                // 例如标记机器需要保存或更新客户端
            }
        };
    }

    /**
     * 计算输出容量
     * @param stack 要插入的物品堆栈
     * @param slot 槽位索引
     * @return 可插入的数量
     */
    public int calculateOutputCapacity(ItemStack stack, int slot) {
        if (!canInsertItem(stack, slot)) {
            return 0;
        }

        ItemStack current = itemHandler.getStackInSlot(slot);

        if (current.isEmpty()) {
            return stack.getMaxStackSize();
        }

        if (ItemStack.isSameItemSameTags(current, stack)) {
            return current.getMaxStackSize() - current.getCount();
        }

        return 0;
    }

    /**
     * 清空所有槽位
     */
    public void clear() {
        for (int i = 0; i < itemHandler.getSlots(); i++) {
            itemHandler.setStackInSlot(i, ItemStack.EMPTY);
        }
    }

    /**
     * 获取指定槽位的物品
     * @param slot 槽位索引
     * @return 物品堆栈
     */
    public ItemStack getStackInSlot(int slot) {
        if (slot < 0 || slot >= itemHandler.getSlots()) {
            return ItemStack.EMPTY;
        }
        return itemHandler.getStackInSlot(slot);
    }

    /**
     * 设置指定槽位的物品
     * @param slot 槽位索引
     * @param stack 物品堆栈
     */
    public void setStackInSlot(int slot, ItemStack stack) {
        if (slot >= 0 && slot < itemHandler.getSlots()) {
            itemHandler.setStackInSlot(slot, stack);
        }
    }

    /**
     * 检查所有输入槽是否为空
     * @return 是否所有输入槽都为空
     */
    public boolean areInputSlotsEmpty() {
        for (int slot : inputSlots) {
            if (!getStackInSlot(slot).isEmpty()) {
                return false;
            }
        }
        return true;
    }

    /**
     * 检查所有输出槽是否为空
     * @return 是否所有输出槽都为空
     */
    public boolean areOutputSlotsEmpty() {
        for (int slot : outputSlots) {
            if (!getStackInSlot(slot).isEmpty()) {
                return false;
            }
        }
        return true;
    }
}